<template>
  <view class="game-header">
    <!-- 左侧操作区 -->
    <view class="header-left">
      <button class="header-btn back-btn" @tap="handleBack">
        <text class="btn-icon">←</text>
        <text class="btn-text">返回</text>
      </button>
    </view>
    
    <!-- 中间标题 -->
    <view class="header-center">
      <text class="game-title">溢彩画</text>
      <text class="level-info">关卡 {{ currentLevel }}</text>
    </view>
    
    <!-- 右侧操作区 -->
    <view class="header-right">
      <button class="header-btn save-btn" @tap="handleSave">
        <text class="btn-icon">💾</text>
      </button>
      <button class="header-btn exit-btn" @tap="handleExit">
        <text class="btn-icon">✕</text>
      </button>
    </view>
    
    <!-- 保存确认弹窗 -->
    <view class="modal-mask" v-if="showSaveModal">
      <view class="save-modal">
        <text class="modal-title">保存游戏</text>
        <text class="modal-content">是否保存当前游戏进度？</text>
        <view class="modal-buttons">
          <button class="modal-btn primary" @tap="confirmSave">保存</button>
          <button class="modal-btn secondary" @tap="cancelSave">取消</button>
        </view>
      </view>
    </view>
    
    <!-- 退出确认弹窗 -->
    <view class="modal-mask" v-if="showExitModal">
      <view class="exit-modal">
        <text class="modal-title">退出游戏</text>
        <text class="modal-content">确定要退出当前游戏吗？</text>
        <view class="modal-buttons">
          <button class="modal-btn danger" @tap="confirmExit">退出</button>
          <button class="modal-btn secondary" @tap="cancelExit">取消</button>
        </view>
      </view>
    </view>
    
    <!-- 保存成功提示 -->
    <view class="toast" v-if="showSaveSuccess">
      <text class="toast-text">保存成功！</text>
    </view>
  </view>
</template>

<script setup>
import { ref, computed } from 'vue'
import { useGameStore } from '@/store/game'
import { useRouter } from 'vue-router'

const props = defineProps({
  currentLevel: {
    type: Number,
    default: 1
  }
})

const emit = defineEmits(['save', 'exit', 'back'])

const gameStore = useGameStore()
const router = useRouter()

// 状态管理
const showSaveModal = ref(false)
const showExitModal = ref(false)
const showSaveSuccess = ref(false)

// 事件处理
const handleBack = () => {
  // 小程序返回逻辑
  if (getCurrentPages().length > 1) {
    uni.navigateBack()
  } else {
    router.push('/pages/level-select/level-select')
  }
  emit('back')
}

const handleSave = () => {
  showSaveModal.value = true
}

const handleExit = () => {
  showExitModal.value = true
}

const confirmSave = () => {
  // 保存游戏状态
  gameStore.saveGameState()
  showSaveModal.value = false
  
  // 显示保存成功提示
  showSaveSuccess.value = true
  setTimeout(() => {
    showSaveSuccess.value = false
  }, 2000)
  
  emit('save')
}

const cancelSave = () => {
  showSaveModal.value = false
}

const confirmExit = () => {
  showExitModal.value = false
  
  // 自动保存退出前的状态
  gameStore.saveGameState()
  
  // 退出到关卡选择页
  router.push('/pages/level-select/level-select')
  emit('exit')
}

const cancelExit = () => {
  showExitModal.value = false
}

// 小程序生命周期处理
onShow(() => {
  // 页面显示时恢复游戏状态
  gameStore.initGameState()
})

onHide(() => {
  // 页面隐藏时自动保存
  gameStore.saveGameState()
})

// 监听返回键（小程序专用）
onBackPress(() => {
  if (showSaveModal.value || showExitModal.value) {
    showSaveModal.value = false
    showExitModal.value = false
    return true
  }
  
  // 显示退出确认
  handleExit()
  return true
})
</script>

<style scoped>
.game-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 20rpx 32rpx;
  background: linear-gradient(135deg, #1a1a2e 0%, #16213e 100%);
  border-bottom: 2rpx solid rgba(108, 92, 231, 0.3);
  box-shadow: 0 4rpx 12rpx rgba(0, 0, 0, 0.3);
}

.header-left {
  flex: 1;
}

.header-center {
  flex: 2;
  text-align: center;
}

.header-right {
  flex: 1;
  display: flex;
  justify-content: flex-end;
  gap: 16rpx;
}

.game-title {
  display: block;
  font-size: 36rpx;
  font-weight: bold;
  color: #fff;
  margin-bottom: 4rpx;
}

.level-info {
  font-size: 24rpx;
  color: #6C5CE7;
}

.header-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16rpx 24rpx;
  background: rgba(255, 255, 255, 0.1);
  border: 1rpx solid rgba(255, 255, 255, 0.2);
  border-radius: 12rpx;
  color: #fff;
  font-size: 28rpx;
  transition: all 0.3s ease;
}

.header-btn:active {
  transform: scale(0.95);
  background: rgba(255, 255, 255, 0.2);
}

.back-btn {
  padding: 16rpx 20rpx;
}

.exit-btn {
  padding: 16rpx;
  background: rgba(255, 99, 132, 0.2);
  border-color: rgba(255, 99, 132, 0.4);
}

.exit-btn:active {
  background: rgba(255, 99, 132, 0.4);
}

.save-btn {
  padding: 16rpx;
  background: rgba(76, 175, 80, 0.2);
  border-color: rgba(76, 175, 80, 0.4);
}

.save-btn:active {
  background: rgba(76, 175, 80, 0.4);
}

.btn-icon {
  font-size: 32rpx;
  margin-right: 8rpx;
}

.btn-text {
  font-size: 26rpx;
}

/* 模态框样式 */
.modal-mask {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: rgba(0, 0, 0, 0.7);
  display: flex;
  justify-content: center;
  align-items: center;
  z-index: 1000;
}

.save-modal,
.exit-modal {
  background: linear-gradient(135deg, #1a1a2e 0%, #16213e 100%);
  border-radius: 24rpx;
  padding: 48rpx;
  width: 80%;
  max-width: 400rpx;
  text-align: center;
  border: 2rpx solid rgba(108, 92, 231, 0.3);
  box-shadow: 0 8rpx 32rpx rgba(0, 0, 0, 0.5);
}

.modal-title {
  display: block;
  font-size: 36rpx;
  font-weight: bold;
  color: #fff;
  margin-bottom: 24rpx;
}

.modal-content {
  display: block;
  font-size: 28rpx;
  color: #ccc;
  margin-bottom: 48rpx;
  line-height: 1.5;
}

.modal-buttons {
  display: flex;
  gap: 24rpx;
}

.modal-btn {
  flex: 1;
  padding: 24rpx;
  border-radius: 12rpx;
  font-size: 28rpx;
  font-weight: 500;
  border: none;
  transition: all 0.3s ease;
}

.modal-btn.primary {
  background: linear-gradient(135deg, #6C5CE7 0%, #5A4FCF 100%);
  color: #fff;
}

.modal-btn.danger {
  background: linear-gradient(135deg, #FF6384 0%, #FF4560 100%);
  color: #fff;
}

.modal-btn.secondary {
  background: rgba(255, 255, 255, 0.1);
  color: #fff;
  border: 1rpx solid rgba(255, 255, 255, 0.3);
}

.modal-btn:active {
  transform: scale(0.95);
}

/* 保存成功提示 */
.toast {
  position: fixed;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  background: rgba(76, 175, 80, 0.9);
  color: #fff;
  padding: 24rpx 48rpx;
  border-radius: 12rpx;
  font-size: 28rpx;
  z-index: 1001;
  animation: fadeInOut 2s ease-in-out;
}

@keyframes fadeInOut {
  0% { opacity: 0; transform: translate(-50%, -50%) scale(0.8); }
  20% { opacity: 1; transform: translate(-50%, -50%) scale(1); }
  80% { opacity: 1; transform: translate(-50%, -50%) scale(1); }
  100% { opacity: 0; transform: translate(-50%, -50%) scale(0.8); }
}

/* 响应式设计 */
@media screen and (max-width: 375px) {
  .game-header {
    padding: 16rpx 24rpx;
  }
  
  .header-btn {
    padding: 12rpx 16rpx;
    font-size: 24rpx;
  }
  
  .game-title {
    font-size: 32rpx;
  }
  
  .level-info {
    font-size: 22rpx;
  }
}
</style>